package com.sdhs.projectschedule.service.impl;

import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.JsonObject;
import com.sdhs.businessfile.domain.BusinessFile;
import com.sdhs.businessfile.service.IBusinessFileService;
import com.sdhs.common.core.domain.event.ProcessEvent;
import com.sdhs.common.enums.BusinessStatusEnum;
import com.sdhs.common.enums.BusinessTypeEnum;
import com.sdhs.common.utils.*;
import com.sdhs.myproject.domain.GcMyprojectInformation;
import com.sdhs.myproject.service.IGcMyprojectInformationService;
import com.sdhs.projectschedule.domain.GcProgressHandIn;
import com.sdhs.projectschedule.service.IGcProgressHandInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sdhs.projectschedule.mapper.GcProgressCompletionOnMapper;
import com.sdhs.projectschedule.domain.GcProgressCompletionOn;
import com.sdhs.projectschedule.service.IGcProgressCompletionOnService;

/**
 * 项目进度控制--竣工Service业务层处理
 *
 * @author andong
 * @date 2025-03-11
 */
@Service
public class GcProgressCompletionOnServiceImpl implements IGcProgressCompletionOnService
{
    @Autowired
    private GcProgressCompletionOnMapper gcProgressCompletionOnMapper;

    @Autowired
    private IBusinessFileService businessFileService;
    @Autowired
    private IGcMyprojectInformationService gcInformationService;

    @Autowired
    private IGcProgressHandInService gcProgressHandInService;
    /**
     * 查询项目进度控制--竣工
     *
     * @param id 项目进度控制--竣工主键
     * @return 项目进度控制--竣工
     */
    @Override
    public GcProgressCompletionOn selectGcProgressCompletionOnById(Long id)
    {
        GcProgressCompletionOn gcProgressCompletionOn = gcProgressCompletionOnMapper.selectGcProgressCompletionOnById(id);
        List<BusinessFile> businessFiles = businessFileService.listByBusinessTypeAndId(BusinessTypeEnum.GC_PROGRESS_COMPLETION_ON.getCode(), String.valueOf(id));
        gcProgressCompletionOn.setFileList(businessFiles);
        return gcProgressCompletionOn;
    }

    /**
     * 查询项目进度控制--竣工列表
     *
     * @param gcProgressCompletionOn 项目进度控制--竣工
     * @return 项目进度控制--竣工
     */
    @Override
    public List<GcProgressCompletionOn> selectGcProgressCompletionOnList(GcProgressCompletionOn gcProgressCompletionOn)
    {
        return gcProgressCompletionOnMapper.selectGcProgressCompletionOnList(gcProgressCompletionOn);
    }

    /**
     * 新增项目进度控制--竣工
     *
     * @param gcProgressCompletionOn 项目进度控制--竣工
     * @return 结果
     */
    @Override
    public GcProgressCompletionOn insertGcProgressCompletionOn(GcProgressCompletionOn gcProgressCompletionOn)
    {
        gcProgressCompletionOn.setCreater(SecurityUtils.getUsername());
        gcProgressCompletionOn.setCreateTime(DateUtils.getNowDate());
        gcProgressCompletionOn.setApprovalStatus(BusinessStatusEnum.DRAFT.getStatusValue());
       gcProgressCompletionOnMapper.insertGcProgressCompletionOn(gcProgressCompletionOn);
        handler(gcProgressCompletionOn.getId(), gcProgressCompletionOn);
        return gcProgressCompletionOn;
    }

    /**
     * 修改项目进度控制--竣工
     *
     * @param gcProgressCompletionOn 项目进度控制--竣工
     * @return 结果
     */
    @Override
    public int updateGcProgressCompletionOn(GcProgressCompletionOn gcProgressCompletionOn)
    {
        gcProgressCompletionOn.setUpdater(SecurityUtils.getUsername());
        gcProgressCompletionOn.setUpdateTime(DateUtils.getNowDate());
        int i = gcProgressCompletionOnMapper.updateGcProgressCompletionOn(gcProgressCompletionOn);
        handler(gcProgressCompletionOn.getId(), gcProgressCompletionOn);
        return i;
    }

    /**
     * 批量删除项目进度控制--竣工
     *
     * @param ids 需要删除的项目进度控制--竣工主键
     * @return 结果
     */
    @Override
    public int deleteGcProgressCompletionOnByIds(Long[] ids)
    {
        return gcProgressCompletionOnMapper.deleteGcProgressCompletionOnByIds(ids);
    }

    /**
     * 删除项目进度控制--竣工信息
     *
     * @param id 项目进度控制--竣工主键
     * @return 结果
     */
    @Override
    public int deleteGcProgressCompletionOnById(Long id)
    {
        GcProgressCompletionOn gcProgressCompletionOn = gcProgressCompletionOnMapper.selectGcProgressCompletionOnById(id);
        gcProgressCompletionOn.setIsDelete("1");
        return gcProgressCompletionOnMapper.updateGcProgressCompletionOn(gcProgressCompletionOn);
    }

    /**
     * 保存联合数据
     * 保存合同附件
     */
    public void handler(Long id, GcProgressCompletionOn gcProgressCompletionOn) {
        // 保存合同附件
        businessFileService.saveFileBatch(BusinessTypeEnum.GC_PROGRESS_COMPLETION_ON.getCode(), String.valueOf(id), gcProgressCompletionOn.getFileList());
    }

    /**
     项目竣工审批监听
     */
    @org.springframework.context.event.EventListener(condition = "#processEvent.key.startsWith('progress_completion_on')")
    public void processHandler(ProcessEvent processEvent) {

        // 已完成（已通过）
        if (BusinessStatusEnum.FINISH.getStatus().equals(processEvent.getStatus())) {
            String businessKey = processEvent.getBusinessKey();
            GcProgressCompletionOn gcProgressCompletionOn = gcProgressCompletionOnMapper.selectGcCompletionOnByInstanceId(businessKey);
            GcMyprojectInformation gcMyprojectInformation = new GcMyprojectInformation();
            gcMyprojectInformation.setId(gcProgressCompletionOn.getProjectId());
            gcMyprojectInformation.setProjectStatus("4");

            GcProgressHandIn gcProgressHandIn = new GcProgressHandIn();
            gcProgressHandIn.setProjectId(gcProgressCompletionOn.getProjectId());
            gcProgressHandIn.setPeriod("1");
            gcProgressHandInService.updatePeriod(gcProgressHandIn);
            /*修改项目状态为已竣工*/
            gcInformationService.updateGcMyProject(gcMyprojectInformation);

            /*推送安全项目开始*/
            if (gcProgressCompletionOn.getProjectId()!=null){
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("engineeringId", gcProgressCompletionOn.getProjectId()); //项目id
                jsonObject.addProperty("type", "update"); //类型
                jsonObject.addProperty("status", "4"); //项目状态-竣工
                try {
                    String jsonData = jsonObject.toString(); // 直接转为JSON字符串
                    String encryptedData = AESUtils.encrypt(jsonData);
                    /*推送安全接口*/
                    SafetyUtil.pushUrl(encryptedData);
                } catch (JsonProcessingException e) {
                    System.err.println("JSON序列化失败: " + e.getMessage());
                    throw new RuntimeException(e);
                } catch (Exception e) {
                    System.err.println("加密失败: " + e.getMessage());
                    throw new RuntimeException(e);
                }
            }
            /*推送安全项目结束*/
        }

    }
}
